<?php
namespace app\api\controller;
header('content-type:application:json;charset=utf8');  
header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Credentials:true');  
header('Access-Control-Allow-Methods:*');  
header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
use app\common\controller\Api;
use think\Request;
use think\Db;

class Userinfo extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * @title 提交用户信息
     * @url /api/Userinfo/SetUserInfo
     * @method post
     * @param name:username type:string require:1 desc:用户名
     * @param name:phone type:string require:1 desc:手机号
     * @param name:idcard type:string require:1 desc:身份证号
     * @return UserName:用户名
     * @return Phone:手机号
     * @return IdCard:身份证号
     */
    public function SetUserInfo(){
        if(Request::instance()->post()){
            $name = Request::instance()->post('username');
            $phone = Request::instance()->post('phone');
            $idcard = Request::instance()->post('idcard');
            if(!$name || !$phone || !$idcard){
                $this->error('参数错误',false,'1003','json');
            }
            $mobile = Db::name('userinfo')->where('phone','=',$phone)->find();
            if($mobile){
                $this->error('此号码已领取',false,'1003','json');
            }
            $card = Db::name('userinfo')->where('idcard','=',$idcard)->find();
            if($card){
                $this->error('此身份证号已领取',false,'1003','json');
            }
            $time = strtotime(date('Y-m-d H:i:s'));
            $enddate = strtotime('2019-10-26 23:59:59');
            $count = Db::name('userinfo')->count();
            if($time > $enddate || $count >= 10000){
                $this->error('活动已结束',false,'1003','json');
            }
            $data = [
                'username' => $name,
                'phone' => $phone,
                'idcard' => $idcard,
                'createtime' => $time,
            ];
            $res = Db::name('userinfo')->insert($data);
            if($res){
                $arr['UserName'] = $name;
                $arr['Phone'] = $phone;
                $arr['IdCard'] = $idcard;
                $this->success('提交成功',$arr,'1001','json');
            }else{
                $this->error('网络错误',false,'1003','json');
            }
        }else{
            $this->error('参数错误',false,'1003','json');
        }
    }







}







?>